package com.whirl.service.impl;

import com.whirl.entity.Forecast;
import com.whirl.entity.Weather;
import com.whirl.mapper.WeatherMapper;
import com.whirl.service.WeatherService;
import com.whirl.util.NumUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author xw
 * @date 2022/8/12 13:09
 */
@Service
public class WeatherServiceImpl implements WeatherService {

    @Resource
    WeatherMapper weatherMapper;

    @Override
    public List<Weather> getWeather() {
        return weatherMapper.getWeather();
    }

    public void processWeather(List<Forecast> forecasts,String filePath ) {
        // 对预测的格式进行处理
        List<Weather> weatherList = new ArrayList<>();
        for (Forecast forecast : forecasts) {
            Weather weather = new Weather();

            String time = forecast.getFxDate();
            String tempMax = forecast.getTempMax();
            String tempMin = forecast.getTempMin();
            String humidity = forecast.getHumidity();

            double tempMax_double = Double.parseDouble(tempMax);
            double tempMin_double = Double.parseDouble(tempMin);
            double humidity_double = Double.parseDouble(humidity);
            double tempAvg = (tempMax_double + tempMin_double) / 2;
            double temperature = NumUtils.stringToDoubleWithTwoDecimal(tempAvg);

            weather.setTime(time);
            weather.setTemperature(temperature);
            weather.setHumidity(humidity_double);
//            System.out.println("weather: " + weather);

            weatherList.add(weather);
//            System.out.println("weatherList: " + weatherList);
        }

        // 写入内容  “占位负载率  100:机房记号  010:温度  001:湿度”
//        String content = "0.70  100:100  010:30.84  001:77.05";

        int length = weatherList.size();

        String rate = "0";
        String room_c66 = "100";  // 001代表d9e8机房   100代表c66机房
        String room_d9e8 = "001";
        String [] temperature = new String[length];
        String [] humidity = new String[length];

        for (int i = 0; i < length; i++) {
            temperature[i] = weatherList.get(i).getTemperature().toString();
            humidity[i] = weatherList.get(i).getHumidity().toString();
        }
//        System.out.println("temperature: " + Arrays.toString(temperature));  // temperature: [34.0, 34.5, 36.0]
//        System.out.println("humidity: " + Arrays.toString(humidity));       // humidity: [63.0, 63.0, 58.0]
        String [] temp_content = new String[length];

        // 两个机房，两组预测
        for (int i = 0; i < length; i++) {
            temp_content[i] = rate + " 100:" + room_c66 + " 010:" + temperature[i] + " 001:" + humidity[i];
        }
        String content = String.join("\n", temp_content);
//        System.out.println("content: \n" + joined);
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))) {
            bufferedWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 两个机房，两组预测
        for (int i = 0; i < length; i++) {
            temp_content[i] = rate + " 100:" + room_d9e8 + " 010:" + temperature[i] + " 001:" + humidity[i];
        }
        content = "\n" + String.join("\n", temp_content);

        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath, true))) {
            bufferedWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将天气数据存入数据库
        weatherMapper.batchInsertWeather(weatherList);
    }
}
